package cn.hf.mychat.service.Impl;

import cn.hf.mychat.config.Result;
import cn.hf.mychat.entity.bo.RequestUser;
import cn.hf.mychat.entity.po.TbSysUser;
import cn.hf.mychat.mapper.UserMapper;
import cn.hf.mychat.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService{

    private final UserMapper userMapper;
    @Value("${test.name}")
    private String time;
    @Override
    public Integer register(RequestUser requestUser) {
        TbSysUser tbSysUser = new TbSysUser();
        tbSysUser.setCreateTime(new Date());
        tbSysUser.setUpdateTime(new Date());
        BeanUtils.copyProperties(requestUser,tbSysUser);
        return userMapper.insert(tbSysUser);
    }

    @Async
    @Override
    public void async() {
        try {
            Thread.sleep(5000);
            System.out.println("异步执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TbSysUser> findAll() {
        return this.userMapper.findAll();
    }

    @Override
    public int updateAll(List<TbSysUser> tbSysUsers) {
        return this.userMapper.updateAll(tbSysUsers);
    }

    @Override
    public void findByTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse;
        try {
            parse = format.parse(time);
            List<TbSysUser> tbSysUser = this.userMapper.findByTime(parse);
            System.out.println("tbSysUser = " + tbSysUser);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        /*List<TbSysUser> tbSysUser = this.userMapper.findByTime(time);
        System.out.println("tbSysUser = " + tbSysUser);*/

    }

    @Override
    public List<TbSysUser> testWrapper(TbSysUser tbSysUser) {
        QueryWrapper<TbSysUser> wrapper = new QueryWrapper<>();
       // UpdateWrapper<TbSysUser> wrapper1 = new UpdateWrapper<>();
      /*  LambdaQueryWrapper<TbSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbSysUser::getPhone,tbSysUser.getPhone()).orderByDesc(TbSysUser::getCreateTime);*/
        List<TbSysUser> tbSysUsers = this.userMapper.selectList(wrapper);
        return tbSysUsers;
    }

    @Override
    public void encrypt(TbSysUser tbSysUser) {
        tbSysUser.setCreateTime(new Date());
        tbSysUser.setUpdateTime(new Date());
        this.userMapper.inserts(tbSysUser);
    }


}
